
export default class CMap {

    obj:any = {}

    constructor(){
        this.clear()
    }

    get(key:string){
        return this.obj[key]
    }

    has(key:string){
        return this.get(key) != null
    }

    set(key:string,value:any){
        this.obj[key] = value
    }

    /**
     *  遍历
     * @param callback-回调value,key,index 
     * @param isBack - 提供的回调函数是否有返回值，返回为true时终止遍历
     * @returns 
     * @example
     * cmp.forEach((value:any,key:any,index:any)=>{
     *  console.log(value)
     *  if(value=="3"){
     *      return true
     *  }
     *  return false
     * },true)
     */
    forEach(callback:Function,isBack:boolean = false){
        let index:number = 0
        for(let key  in this.obj){
            if(isBack){
                if(callback(this.obj[key],key,index)){
                    // console.log("终止遍历");
                    return
                }
            }else{
                callback(this.obj[key],key,index)
            }
            index++
        }
    }

    keys(){
        return Object.keys(this.obj)
    }

    values(){
        return Object.values(this.obj)
    }

    remove(key:string){
        delete this.obj[key]
    }

    size(){
        return this.keys().length
    }

    clear(){
        this.obj = {}
    }

    max(num:number = 5000){
        if(this.keys().length>num){
            this.clear()
        }
    }

}